<?php
namespace app\ad\controller;
use framework\ext\Check;
/* *
 * 广告管理
 * */
class IndexController extends \app\base\controller\AdminController{

    //文章列表
    public function index(){
        $act = form('get.act');
        if($act == 'del'){
            $id = form('get.id',0,intval);
            $result = model('index')->_delete($id);
            if($result){
                $this->jsonMsg('删除成功',1);
            }else{
                $this->jsonMsg('删除失败,请查看是否包含广告资源');
            }
        }else{
            $this->cid = form('get.id',0,intval);
            $page = form('get.page',0,intval);
            $this->list = model('index')->pager($page,20)->_select($this->website_id);
            $this->pager = getPage(model('index')->pager);
            $this->display();
        }
    }

    //编辑
    public function edit(){
        if($this->isPost()){
            $msg = Check::rule(array(check::must($_POST['title']),'广告位名称必须填写'));
            if(true !== $msg){
                $this->pageMsg($msg);
            }
            $data = array();
            $data = form('post',array('fid','starttime','endtime'));
            $data['uptime'] = time();
            $data['starttime'] = strtotime(form('post.starttime'));
            $data['endtime'] = strtotime(form('post.endtime'));
            $data['website_id'] = $this->website_id?$this->website_id:form('post.website_id',0,intval);
            $fid = form('post.fid',0,intval);
            if($fid){
                $condition['id'] = $fid;
                $rel = model('index')->_update($data,$condition);
            }else{
                $rel = model('index')->_update($data);
            }
            if($rel){
                $this->jsonMsg('自定义页面设置成功',1,url('index/index'));
            }else{
                $this->jsonMsg('自定义页面设置失败');
            }
        }else{
            $this->fid = form('get.id',0,intval);
            $this->info = model('index')->_find(array('id' =>$this->fid));
            $this->display();
        }
    }

    public function preview(){
        $id = form('get.id',0,intval);
        $this->lists = model('adlable')->index($id);
        if(!$this->lists){
            $this->pageMsg('广告已过期');
        }
        $this->display();
    }
}